package com.hjl.web.servlet;

import com.hjl.domain.Anno_inf;
import com.hjl.domain.PageBean;
import com.hjl.service.AnnoService;
import com.hjl.service.impl.AnnoServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * 公告表现层
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 11:26
 */
@WebServlet("/annos/*")
public class AnnoServlet extends BaseServlet {
    private AnnoService annoService = new AnnoServiceImpl();
    String prefix = "/WEB-INF/jsp/anno/";
    String suffix = ".jsp";

    /**
     * 跳转WEB-INF里的jsp
     */
    public void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "annolist" + suffix).forward(req, resp);
    }

    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "annoadd" + suffix).forward(req, resp);
    }

    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(prefix + "annoedit" + suffix).forward(req, resp);
    }

    /**
     * 分页
     */
    public void findAnnoByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获取数据（当前页码和行数）
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        //默认第1页，5行
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)) {
            rows = "5";
        }
        Map<String, String[]> condition = req.getParameterMap();
        PageBean<Anno_inf> pb = annoService.findAnnoByPage(currentPage, rows, condition);
        req.setAttribute("annos", pb);
        req.setAttribute("condition", condition);

        req.getRequestDispatcher("/annos/toList").forward(req, resp);
    }

    /**
     * 删除选中
     */
    public void delSelected(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String[] ids = req.getParameterValues("id");
        annoService.delSelect(ids);
        resp.sendRedirect(req.getContextPath() + "/annos/findAnnoByPage");
    }

    /**
     * 查询单个（用于编辑）
     */
    public void findAnno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String id = req.getParameter("id");
        //调用service方法
        Anno_inf anno = annoService.findAnnoById(id);
        //存入request
        req.setAttribute("anno", anno);
        System.out.println(anno);
        //转发到annoedit.jsp
        req.getRequestDispatcher("/annos/toEdit").forward(req, resp);
    }

    /**
     * 更新
     */
    public void updateAnno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //编码
        req.setCharacterEncoding("utf-8");
        //获取map
        Map<String, String[]> map = req.getParameterMap();
        Anno_inf anno = new Anno_inf();
        try {
            //封装
            BeanUtils.populate(anno, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
//        调用service方法
        annoService.updateAnno(anno);
        //重定向回去
        resp.sendRedirect(req.getContextPath() + "/annos/findAnnoByPage");
    }

    /**
     * 添加
     */
    public void addAnno(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        //获得参数
        Map<String, String[]> map = req.getParameterMap();
        //封装对象
        Anno_inf anno = new Anno_inf();
        try {
            BeanUtils.populate(anno, map);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service保存
        annoService.addAnno(anno);
        //跳转到findAnnoByPage
        resp.sendRedirect(req.getContextPath() + "/annos/findAnnoByPage");
    }
}
